package cc.dawning.utiming.lklocation.location.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * 权限处理
 * Created by nanPengFei on 2016/12/21 14:11.
 */
public class LocationPermissionUtil {
    private static LocationPermissionUtil mInstance;

    private LocationPermissionUtil() {
    }

    public static LocationPermissionUtil getInstance() {
        if (null == mInstance) {
            synchronized (LocationPermissionUtil.class) {
                if (null == mInstance) {
                    mInstance = new LocationPermissionUtil();
                }
            }
        }
        return mInstance;
    }


    /**
     * 判断是否拥有某种权限,第一次没有则弹窗询问,用户拒绝后，
     * 请到对应的Activity中重写onRequestPermissionsResult进行判断requestCode值进行相应提示
     *
     * @param activity
     * @param permission
     * @param requestCode
     * @return
     */
    private boolean requestPermission(Activity activity, String[] permission, int requestCode) {
        boolean isHasPermission = false;//默认没有权限
        if (ContextCompat.checkSelfPermission(activity, permission[0]) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, permission, requestCode);
        } else {
            isHasPermission = true;
        }
        return isHasPermission;
    }

    /*关于权限申请*/
    public static final int REQUEST_PERMISSION_SD = 601;//SD卡权限申请
    public static final int REQUEST_PERMISSION_LOCATION = 602;//定位权限申请
    public static final int REQUEST_PERMISSION_FILESYSTEMS = 603;//挂载SD卡权限申请

    /**
     * SD卡权限申请
     *
     * @param activity
     * @return
     */
    public boolean requestSD(Activity activity) {
        return requestPermission(activity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                }, REQUEST_PERMISSION_SD);
    }
    /**
     * 定位权限申请
     *
     * @param activity
     * @return
     */
    public boolean requestLocation(Activity activity) {
        return requestPermission(activity,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION
                }, REQUEST_PERMISSION_LOCATION);
    }
    /**
     * 挂载SD卡权限申请
     *
     * @param activity
     * @return
     */
    public boolean requestFilesystems(Activity activity) {
        return requestPermission(activity,
                new String[]{Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS
                }, REQUEST_PERMISSION_FILESYSTEMS);
    }
    /**
     * 申请提供的所有权限
     *
     * @param activity
     */
    public void requestAll(Activity activity) {
        requestPermission(activity,
                new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_PERMISSION_FILESYSTEMS);
    }
}
